#!/bin/bash

# WebP Toolbox 浏览器插件构建脚本
# 作者: AI Assistant
# 版本: 1.0.0

echo "🚀 开始构建 WebP Toolbox 浏览器插件..."

# 检查Node.js环境
if ! command -v node &> /dev/null; then
    echo "❌ 错误: 未找到 Node.js，请先安装 Node.js"
    exit 1
fi

if ! command -v npm &> /dev/null; then
    echo "❌ 错误: 未找到 npm，请先安装 npm"
    exit 1
fi

# 检查依赖
echo "📦 检查依赖..."
if [ ! -d "node_modules" ]; then
    echo "📥 安装依赖..."
    npm install
fi

# 清理之前的构建
echo "🧹 清理之前的构建..."
rm -rf dist
rm -f dist.zip

# 构建项目
echo "🔨 构建项目..."
npm run build

if [ $? -ne 0 ]; then
    echo "❌ 构建失败"
    exit 1
fi

# 复制必要文件
echo "📋 复制插件文件..."
cp public/manifest.json dist/
cp public/favicon.ico dist/

# 创建压缩包
echo "📦 创建压缩包..."
zip -r dist.zip dist/

if [ $? -ne 0 ]; then
    echo "❌ 压缩失败"
    exit 1
fi

# 移动到部署目录
echo "📁 移动到部署目录..."
mkdir -p webp-toolbox-deploy
mv dist.zip webp-toolbox-deploy/

# 显示构建结果
echo ""
echo "✅ 构建完成！"
echo ""
echo "📊 构建信息:"
echo "   - 构建时间: $(date)"
echo "   - 插件包: webp-toolbox-deploy/dist.zip"
echo "   - 插件大小: $(du -h webp-toolbox-deploy/dist.zip | cut -f1)"
echo ""
echo "📋 安装步骤:"
echo "   1. 解压 dist.zip"
echo "   2. 打开 chrome://extensions/"
echo "   3. 启用开发者模式"
echo "   4. 点击'加载已解压的扩展程序'"
echo "   5. 选择解压后的 dist 文件夹"
echo ""
echo "🎯 插件功能:"
echo "   - WebP图片转换"
echo "   - JSON格式化"
echo "   - Base64编码/解码"
echo "   - 时间戳工具"
echo "   - 文本对比"
echo "   - API测试"
echo "   - 备忘录"
echo "   - 访问统计"
echo ""
echo "⌨️ 快捷键:"
echo "   - Ctrl+F: 切换悬浮窗"
echo "   - Ctrl+Shift+F: 重置悬浮窗"
echo ""
echo "🔧 开发模式:"
echo "   - 修改代码后运行: npm run build"
echo "   - 在扩展页面点击'重新加载'"
echo ""
echo "📖 详细说明请查看: webp-toolbox-deploy/CHROME_EXTENSION_INSTALL.md" 